var cLOGIN_SERVICES = "services.asmx";

/*************************************************************
* Function		: alternateTbl
* Input			: id - Table id
* Details		: Changes the style of rows in the table, 
*				  every second row gets the same style
*************************************************************/
function alternateTbl(id){ 
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);   
   var rows = table.getElementsByTagName("tr");   
   for(i = 0; i < rows.length; i++){           
     if(i % 2 == 0){  
      rows[i].className = "GrayBG";
     }else{
       rows[i].className = "AlterBG"; 
     }       
   } 
 } 
}

/*************************************************************
* Function		: getQueryStringValue
* Input/Output	: key
* Details		: Get value from query string
*************************************************************/
function getQueryStringValue(key)
{
	key = key.toLowerCase();
	var srch = window.document.location.search;
	var tmpSrch = srch.toLowerCase();
	var iStart = tmpSrch.indexOf(key);
	if (iStart == -1)
		return null;
	iStart += key.length + 1;
	var iEnd = tmpSrch.indexOf("&", iStart);
	if(iEnd == -1)
		iEnd = tmpSrch.length;
	var val = srch.substring(iStart, iEnd);
	return val;
}

/*************************************************************
* Function		: getQueryStringValue
* Input/Output	: key
* Details		: Get value from query string from the _TOP document
*************************************************************/
function getTopQueryStringValue(key)
{
	key = key.toLowerCase();
	var srch = window.top.document.location.search;
	var tmpSrch = srch.toLowerCase();
	var iStart = tmpSrch.indexOf(key);
	if (iStart == -1)
		return null;
	iStart += key.length + 1;
	var iEnd = tmpSrch.indexOf("&", iStart);
	if(iEnd == -1)
		iEnd = tmpSrch.length;
	var val = srch.substring(iStart, iEnd);
	return val;
}

/*************************************************************
*		SOAP request related functions					     *
*************************************************************/
/*************************************************************
* Function		: SOAPRequest
* Input/Output	: None
* Details		: 
*************************************************************/
function SOAPRequest(URL,Namespace,MethodName,Params,func)
{ 
	var Host = document.location.host;
	var str='<?xml version="1.0" encoding="utf-8"?>';                          
	str+='<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
	str+='<soap:Body>';
	str+='<' + MethodName + ' xmlns="http://tempuri.org/' + Namespace + '">';
	if (Params!=null)
	{
		for(var i=0;i<Params.length;i+=2)
		{
			str+='<' + Params[i] + '>' + Params[i+1] + '</' + Params[i] + '>';
		}
	}
	str+='</' + MethodName + '>';
	str+='</soap:Body>';
	str+='</soap:Envelope>';
	
	try
	{
		var objHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		var fault;
		objHTTP.open("POST",URL,(func!=null));
		objHTTP.setRequestHeader("POST",URL);  
		objHTTP.setRequestHeader("Host",Host);  
		objHTTP.setRequestHeader("Content-Type","text/xml; charset=utf-8"); 
		objHTTP.setRequestHeader("Content-Length",str.length);
		objHTTP.setRequestHeader("SOAPAction","http://tempuri.org/" + Namespace + "/" + MethodName);
		if (func!=null) 
		{ 
			objHTTP.onreadystatechange=function() {
				if (objHTTP.readyState==4) {
						if (objHTTP.ResponseXml.selectSingleNode("//" + MethodName + "Result") == null)
						{
							MSGBox(objHTTP.ResponseXml.text ,"Error...");
							return;
						}
						var Result = objHTTP.ResponseXml.selectSingleNode("//" + MethodName + "Result").cloneNode(true);
						if (Result==null)
						{
							Result = Dom.selectSingleNode("//faultstring"); //Fault							
						}
						delete objHTTP;
						func(Result);
				}
			}
			objHTTP.send(str);	
		}
		else
		{
			objHTTP.send(str);	
			var Dom = objHTTP.ResponseXml;
			var Result=Dom.selectSingleNode("//" + MethodName + "Result");
			if (Result==null)
			{
				fault = Dom.selectSingleNode("//faultstring");
				if (fault == null) 
					MSGBox("Please try again later.","System Error...");		
				else
					MSGBox(fault.text);
			}
			delete objHTTP;
			return Result;
		}
	}
	catch(ex)
	{
		if (objHTTP.ResponseText != null) MSGBox(objHTTP.ResponseText + " OR " + ex.message,"Error...");
	}
}

/*************************************************************
* Function           : XmlEncode
* Input/Output       :      
* Details            : 
*************************************************************/

function XmlEncode(sText) {
        sText = sText.replace(/&/g, "&amp;")
        sText = sText.replace(/</g, "&lt;")
        sText = sText.replace(/>/g, "&gt;")
        sText = sText.replace(/'/g, "&apos;")
        sText = sText.replace(/"/g, '&quot;')
        return sText
}
 
/*************************************************************
* Function           : XmlDecode
* Input/Output       :      
* Details            : 
*************************************************************/

function XmlDecode(sText) {
              sText = sText.replace(/&apos;/g, "'")
              sText = sText.replace(/&gt;/g, ">")
              sText = sText.replace(/&lt;/g, "<")
        sText = sText.replace(/&amp;/g, "&")
        sText = sText.replace(/&quot;/g, '"')
       return sText
}


/*************************************************************
* Function		: MSGBox
* Input/Output	: str
* Details		: Displayes message
*************************************************************/
function MSGBox(strMessage,title)
{
	var	screenW = screen.width/2-225;
	var screenH = screen.height/2-140;
	var	dialogW = 365;
	var dialogH = 257;

	if 	(browserName=='Firefox'){
		dialogW = 365;
		dialogH = 263;
	}
	if 	(browserName=='Netscape'){
		dialogW = 365;
		dialogH = 263;
	}
	if 	(browserName=='Opera'){
		dialogW = 365;
		dialogH = 263;
	}
	
	if 	(browserName!='Opera')
		return window.showModalDialog("/include/MESSAGES/ModalErrorMsg.aspx?ErrMsg=" + strMessage + "&UserStyle="+browserName.replace("Microsoft Internet Explorer","IE"), null, 'dialogLeft:'+screenW+'px; dialogTop:'+screenH+'px; dialogWidth:'+dialogW+'px; dialogHeight:'+dialogH+'px; toolbar:0; help: no; status: no;center: yes; scroll: no');
	else
		return window.open("/include/MESSAGES/ModalErrorMsg.aspx?ErrMsg=" + strMessage + "&UserStyle=3",'','left='+screenW+',top='+screenH+',width=365,height=263,modal=yes,center=yes,target=_blank,status=no,toolbar=no, menubar=no, location=no, resizable=no, scrollbars=no')
}

function IsXP()
{
	return (navigator.userAgent.indexOf("5.1") >=0 ? true:false);
}
/*************************************************************
* Function		: InteractWithUser
* Input/Output	: str
* Details		: Displayes message
*************************************************************/
function InteractWithUser(strMessage,title,showBack,ShowYes,ShowNo)
{
	var retVal;
	var oArg = new Object();
	
	if (title == null)
		oArg.title = '';
	else
		oArg.title = title;
	oArg.message = strMessage;
	oArg.Interact = true;
	oArg.MessageBox = false;
	if (showBack){
		oArg.showBack = showBack;
	}
	else{
		oArg.showBack = true;
	}	
	if (ShowYes){
		oArg.ShowYes = ShowYes;
	}	
	else{
		oArg.ShowYes = true;
	}		
	if(ShowNo){
		oArg.ShowNo = ShowNo;
	}
	else{
		oArg.ShowNo = true;
	}	
	
	//retVal = window.showModalDialog("/FX/Common/WebForms/InteractWithUser.aspx?UserStyle="+ getUserStyle(),oArg,"dialogWidth:350px; dialogHeight:180px; help: No; status: No;");	
	retVal = confirm(strMessage);
	
	delete oArg;
	return retVal;
}

/*************************************************************
* Function		: PromptUser
* Input/Output	: key
* Details		: opens prompt us window and returns ansawer
*				  validationFunction accepts one parameter and 
*				  should return boolean
*************************************************************/
function PromptUser(promptText,promptValue,title,validationFunction,validationMSG)
{
	var retVal;
	var oArg = new Object();
	if (title == null)
		oArg.title = '';
	else
		oArg.title = title;
	oArg.promptText = promptText;
	oArg.promptValue = promptValue;
	oArg.validationMSG = validationMSG;
	oArg.validationFunction = validationFunction;
	
	retVal = window.showModalDialog("/FX/Common/WebForms/PromptUser.aspx?UserStyle="+ getUserStyle(),oArg,"dialogWidth:350px; dialogHeight:180px; help: No; status: No;");	
	delete oArg;
	return retVal;
}

//
// Searchs the options in a drop-down-list (<select>)
// for a specific item TEXT and select it if found.
// RETURN: TRUE - if found, FALSE - if not.
function SelectComboBoxItem(cmb,text)
{
	if (text != null)
	{
		var i;
		text = trim(text.toUpperCase());
		for (i=0; i < cmb.options.length; i++)
		{
			if (trim(cmb.options[i].text.toUpperCase()) == text)
			{
				cmb.selectedIndex = i;
				return true
			}
		}
	}
	return false;
}
// Searchs the options in a drop-down-list (<select>)
// for a specific item VALUE and select it if found.
// RETURN: TRUE - if found, FALSE - if not.
function SelectComboBoxItemValue(cmb,value)
{
	if (value != null)
	{
		var i;
		value = trim(value.toUpperCase());
		for (i=0; i < cmb.options.length; i++)
		{
			if (trim(cmb.options[i].value.toUpperCase()) == value)
			{
				cmb.selectedIndex = i;
				return true
			}
		}
	}
	return false;
}

function trim (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

/**********************************************************************/
// Validates that the controls' value isn't "", display a msg if is
// Return: TRUE - If control is NOT empty, FALSE - If control IS empty.
/**********************************************************************/
function validateNotEmpty(ctr,msg)
{
	if (trim(ctr.value) == "") 
	{ 
		MSGBox (msg);
		return false;
	}
	return true;
}

/****************************************************************/
// isEmail (STRING s)
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
/****************************************************************/
function isEmail (s)
{   //if (isEmpty(s)) 
     // if (isEmail.arguments.length == 1) return defaultEmptyOK;
      // else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    //if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

