url_protocol = 'http';
if(location.protocol=='https:')
{
	url_protocol = 'https';
}
if (document.domain.indexOf('sitemind') > 0)
{
	var SITE_ROOT = url_protocol+'://'+document.domain+'/service_america/';
}
else if (document.domain.indexOf('16.139.241.51') > 0) //I removed the 1 from 192.168.116.95 so that if would not have a 0 index. not a great solution
{
	var SITE_ROOT = url_protocol+'://'+document.domain+'/';
}
else
{
	var SITE_ROOT = url_protocol+'://'+document.domain+'/';
}
//clears and replaces text in form input fields and textareas
function clearText(thefield,defaultValue) {
	if(defaultValue=='') { defaultValue = thefield.defaultValue; }
	if (defaultValue==thefield.value) { thefield.value = ""; }
}
function replaceText(thefield,defaultValue) {
	if(defaultValue=='') { defaultValue = thefield.defaultValue; }
	if (thefield.value=="") { thefield.value = defaultValue; }
}

$(document).ready(function() { 
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"]) {
		window.location="ie6.php"; }
	
	$('ul.sf-menu').superfish(); 
	
	$('a.newWin').click(function(){
		window.open(this.href);
		return false;
	});
});



function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

function getQueryString(key)
{
    var re = new RegExp( "[?&]" + key + "=([^&$]*)", "i" );
    var offset = location.search.search( re );
    if ( offset == -1 ) return null;
    return RegExp.$1;
}

function showResult(data, pageName, redirectPageName)
{
	$('#errorMsg').slideUp();
	$("#errorMsg").html('');
	if(data[0].result=="success")
	{
		resetForm();
        if($('#processing').length > 0) $("#processing").show();
    	if($('#btnSubmit').length > 0) $("#btnSubmit").hide();

        if(pageName != '')
        {
	    	$("#step_content").hide();
	        $("#step_content").load(SITE_ROOT+pageName,function(){
	        	if($('#btnSubmit').length > 0) $("#btnSubmit").show();
	        	if($('#processing').length > 0) $("#processing").hide();
	        	$("#step_content").slideDown();}
	        );
        }
        else if(redirectPageName != '') window.location=SITE_ROOT+redirectPageName;
        else
        {
        	if($('#thankyouMsg').length > 0) $("#thankyouMsg").slideDown();
        	if($('#thisForm').length > 0) $("#thisForm").slideUp();
        	if($('.thisForm').length > 0) $(".thisForm").slideUp();
        }
	}
	else
	{      
		showErrors(data);
	}
}

function showErrors(data)
{
	$('.form2col, .form1col, .callout').children().each(function(index){
        if(this.id != 'errorMsg') $(this).removeClass("error");
        $(this).children().each(function(index){
            if(this.id != 'errorMsg') $(this).removeClass("error");
        });
    });
	
	//loop through the JSON array and display the errors
	$.each(data[0], function(key,value) {                    
		if(key != "result")
		{
			if(value==1)
			{
				$('label[for='+key+']').parent().addClass("error");
				$('#'+key).parent().addClass("error");
			}
			else
			{
				$('label[for='+key+']').parent().addClass("error");
				$('#'+key).parent().addClass("error");
				$("#errorMsg").html($("#errorMsg").html()+value);
				$("#errorMsg").slideDown();
			}						
		}
	});
	$.scrollTo(0,800);
	if($('#processing').length > 0) $("#processing").hide();
	if($('#btnSubmit').length > 0) $("#btnSubmit").show();
}

function resetForm()
{
	$('[type="text"]').val("");
	$('textarea').val("");
	$('.form2col, .form1col, .callout').children().each(function(index){
        if(this.id != 'errorMsg') $(this).removeClass("error");
        $(this).children().each(function(index){
            if(this.id != 'errorMsg') $(this).removeClass("error");
        });
    });
}

function submitenter(myfield,submitFunction,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		eval(submitFunction)();
		return false;
	}
	else return true;
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Cyanide_7 |  */
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function confirmDelete(delUrl,itemid,title,deleteType) {
	if (confirm("Are you sure you want to delete the "+deleteType+" called "+title+"?")) {
		$.post(SITE_ROOT+delUrl,
			{ 
				operation: 'deleteItem', 
				id: itemid
			},
			function(data){
				gridReload();
			}, 
			"json"
		);
	}
}

function confirmDeleteMultiple(delUrl,items,deleteType) {
	if (confirm("Are you sure you want to delete the selected "+deleteType+"(s)?")) {
		$.post(SITE_ROOT+delUrl,
			{ 
				operation: 'deleteItem', 
				id: items
			},
			function(data){
				gridReload();
			}, 
			"json"
		);
	}
}

function slideDown_prep(el){
	var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
	  
	if( !height ){
		height = $el.show().height();
		// update the height
		$el.data("originalHeight", height);
		// if the element was hidden, hide it again
		if( !visible ) $el.hide().css({height: 0});
	}
}

function updateLoginStatus()
{
	$('#login_inner').hide();
	$.post(SITE_ROOT+"ajax/customer_process.php",
		{ 
			operation: 'updateLoginStatus'
		},
		function(data){
			$('#login_inner').html(data);
			//$('#login_inner').show();
			//$('#login_inner').show("drop", { direction: "down" }, 1000);
			$('#login_inner').effect("bounce", { times:4, mode: 'show', direction: 'down' }, 300);
			//$('#login_inner').show("puff", { percent:200, mode: 'show'}, 1000);
		}, 
		"html"
	);
}

function explode (delimiter, string, limit) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict; 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {                return true;
            }
        }
    }
     return false;
}
