﻿var psStart = 50000; var psEnd = 9900000; var chkSlect = 3;
var prcWt = false;
var DDList = new Array("City", "State", "Industry", "COUNTRY");
var vCurrList = new Array( "EURO", "USD", "INR", "GBP", "Others");
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function checkSelectionLimit(formEleID, limit){var formEle=document.getElementById(formEleID);var returnVal=true;var count=0;for(var i=0;i<formEle.length;i++){
if(formEle[i].selected){count++;if(count>limit){if(returnVal)alert("You cannot select more than " + limit + " options!");returnVal = false;formEle[i].selected = false;}}}return returnVal;}

function res_populateFarea(formFareaId, formRoleId, limit, psMixValText, psValAsSeq)
{
    var fareaEle=document.getElementById(formFareaId);
    checkSelectionLimit(formFareaId,limit);
    var selI=fareaEle.selectedIndex;
    if(selI<0)return false;
    ClearList(formRoleId);
    var roleEle = document.getElementById(formRoleId);
    var className = roleEle.className;
    var width = roleEle.width;
    var SeqCount=0; 
    limit = ( limit ? limit : chkSlect );
    for(var num=1;num<fareaEle.options.length;num++)
    {
        if( fareaEle.options[num].selected)
        {
            selI=num;
            if(roleEle.length==0)
            { AddOption(formRoleId, "", "--Select--", roleEle.getAttribute("size") == "" || roleEle.getAttribute("size") == "0" || roleEle.getAttribute("size") == null, false, !roleEle.getAttribute("multiple")); }
             if (fareaEle.value!="--0--")
             {
                selI=(isNaN(fareaEle[selI].value)?selI:selI-fareaEle[selI].value );
                var sp1=fareaEle[selI].value.split(",");
                for(var i=0;i<sp1.length;i++)
                {
                    opt=null;
                    if(sp1[i].indexOf("--")>=0)
                    {   AddOption(formRoleId, "--0--", sp1[i], false, true);}
                    else
                    {
                        SeqCount++;
                        AddOption(formRoleId, ( psValAsSeq ? SeqCount : ( psMixValText ? sp1[i] + "(" + fareaEle[selI].text + ")" : sp1[i] ) ), sp1[i], true);
                    }
                }
            }
        }
    }
    roleEle.className=fareaEle.className;
    SelectDateData(formRoleId, null, null, "Any Specialization");
    if( roleEle.options.length == 1 ) { roleEle.options[0].setAttribute("selected", "true"); if(roleEle.options[0].selected != null && roleEle.options[0].selected != true ){roleEle.options[0].selected=true;} }
    return false;
}

    function ChangeColorAndSetIndex(srcCtrlID, bgColor, selIndex, topText)
    {
        var tmp = document.getElementById(srcCtrlID);
        if(tmp == null) { return; }
        if(topText)
        {
            tmp.options[0].text = topText;
            tmp.options[0].innerText = topText;
        }
        if(bgColor != null )
        {
            for(var i = 0; i < tmp.options.length; i++)
            {
                tmp.options[i].style.backgroundColor = bgColor;
            }
        }
        tmp.selectedIndex = selIndex;
    }

function ClearList(srcListBoxId){var fruitList=document.getElementById(srcListBoxId);if(fruitList.length==-1) return;
var eachGroup=fruitList.firstChild;while(eachGroup!=null){fruitList.removeChild(eachGroup);eachGroup = fruitList.firstChild;}}

function SelectDateData(srcList, destHDField, psIndex, matchVal)
{
    var lst = document.getElementById(srcList);
    if( lst == null ) { return; }
    if( psIndex == null ) psIndex = 0;
    var selValues = ( matchVal == null ? (document.getElementById(destHDField).value.split("#"))[psIndex] : matchVal);
    if(selValues == null ) selValues = "";
    if(selValues == ""){lst.selectedIndex = 0; return;}
    if( selValues != "" )  lst.selectedIndex = -1;
    for( var i = 0; i < lst.options.length; i++)
    {
        if( ToUCase(selValues) == ToUCase(lst.options[i].value) || ToUCase(selValues).indexOf(ToUCase(lst.options[i].text)) >= 0 )
        { lst.options[i].setAttribute("selected", "true"); if(lst.options[i].selected != null && lst.options[i].selected != true ) {lst.options[i].selected = true;}}
    }
}

function ToUCase(str)
{
    if( str == null || str == "" ) { return str; }
    return str.toUpperCase()
}

function SaveDataText(srcTBox, destHDField, psValue, psDntErase)
{
    var val = document.getElementById(destHDField);
    if(val == null || document.getElementById(srcTBox) == null ) return;
    if(psDntErase == null || psDntErase == false ) val.value="";
    else val.value += "#";
    val.value += document.getElementById(srcTBox).value;
}



function SaveData(srcList, destHDField, psValue, psDntErase, svAll, incldStart)
{
    var lst = document.getElementById(srcList);
    if(lst == null){return;}
    var val = document.getElementById(destHDField);
    svAll = ( svAll != null ? svAll : false);
    if(psDntErase == null || psDntErase == false ) val.value="";
    else val.value += "#";

    for( var i = (incldStart == null ? 0 : 1 ); i < lst.options.length; i++)
    {
        if( lst.options[i].selected || svAll )
        {
            if( val.value == "" || val.value.lastIndexOf('#') == val.value.length-1)
                val.value += ( psValue ? lst.options[i].value : lst.options[i].text );
            else 
                val.value += "|" + ( psValue ? lst.options[i].value : lst.options[i].text );
        }
    }
}

function SaveData1(srcList, destHDField)
{
    var lst = document.getElementById(srcList);
    if(lst == null){return;}
    var val = document.getElementById(destHDField);
     val.value = "";
    val.value =  lst.options[lst.selectedIndex].text; 
}

function addCommas(nStr)
{
	var rgx; var rlcStr = '$1' + ',' + '$2';
	nStr = nStr.toString();
	if( nStr.length < 6 ){rgx = /(\d+)(\d{3})/;}
	else if ( nStr.length < 8 )
	{
		rgx = /(\d+)(\d{2})(\d{3})/;
		rlcStr += ',' + '$3';
	}
	else
	{
		rgx = /(\d+)(\d{2})(\d{2})(\d{3})/;
		rlcStr += ',' + '$3' + ',' + '$4';
	}
	while (rgx.test(nStr)) 
		nStr = nStr.replace(rgx, rlcStr);
	return nStr;
}

function BindRelatedCtrl( psSrcCtrlID, psDestCtrlID, valDefault, bgYellow, psZeroIndex, psColor)
{
	var src = document.getElementById(psSrcCtrlID);
	if(psZeroIndex == null){if( src.selectedIndex <= 0 ) { return;}}
	ClearList(psDestCtrlID);
	if(valDefault != '' ){AddOption(psDestCtrlID, '', valDefault?valDefault:'--Select--', true, false, psColor==null);}
	for(var i = src.selectedIndex; i < src.options.length; i++)
	{	AddOption(psDestCtrlID, src.options[i].value, src.options[i].text, true, false, bgYellow);}
	return false;
}

function FillSlyExpCtrl(psSrcCtrlID, psExpr, noDefVal, psColor)
{
	ClearList(psSrcCtrlID);
	if(noDefVal == null) { AddOption(psSrcCtrlID, '', '--Select--', true, false, psColor==null); }
	if( !psExpr )
	{
		AddOption(psSrcCtrlID, '0', '< ' + addCommas(psStart), true);
		AddOption(psSrcCtrlID, psStart, addCommas(psStart), true);
	}
	var psSlyInc = (psExpr ? 1 : 100000 );
	var end = ( psExpr ? 30 : psEnd );
	for(var i = (psExpr ? 0 : psSlyInc ); i <= end; i+=psSlyInc)
	{	
		AddOption(psSrcCtrlID, i, addCommas(i), true);
		if( i >= 2000000 ) { psSlyInc = 500000; }
		if( i >= 5000000 ) { psSlyInc = 1000000; }
	}
	if( !psExpr ) AddOption(psSrcCtrlID, psEnd, addCommas(psEnd), true);
	document.getElementById(psSrcCtrlID).selectedIndex = -1;
}

function FillCurrencyCtrl(psSrcID)
{
	AddOption(psSrcID, "", "--Select--", true, false, true);
	for( var i = 0; i < vCurrList.length; i++)  AddOption(psSrcID, vCurrList[i], vCurrList[i], true);
}

function FillMonthCtrl(psSrcID)
{
	AddOption(psSrcID, "", "Month", true, false, true);
	for( var i = 0; i < m_names.length; i++)  AddOption(psSrcID, m_names[i], m_names[i], true);
}

function FillYearCtrl(psSrcID, psLimit)
{
	AddOption(psSrcID, "", "Year", true, false, true);
	for( var i = new Date().getFullYear(); i > new Date().getFullYear() - psLimit; i--)  AddOption(psSrcID, i, i, true);
}

function FillDayCtrl(psSrcID)
{
	AddOption(psSrcID, "", "Day", true, false, true);
	for( var i = 1; i <= 31; i++)  AddOption(psSrcID, i, i, true);
}

function RebuiltListAddOtipnGroup(srcListBoxId)
{
	var fruitList = document.createElement('select');
	var scrLst = document.getElementById(srcListBoxId);
    var psOption = true;

    for( var i = 0; i < scrLst.options.length; i++)
    {
        var opt = document.createElement('option');
        opt.value = scrLst.options[i].value;
        opt.innerText = scrLst.options[i].innerText;
        opt.text = scrLst.options[i].text;
	    fruitList.appendChild(opt);
    }	
    ClearList(srcListBoxId);
    for( var i = 0; i < fruitList.options.length; i++)
    {
        psOption = ( fruitList.options[i].value == "optgroup" ? false : true );
        var opt = ( psOption ? opt=document.createElement('option') : document.createElement('optgroup') );
        opt.value = fruitList.options[i].value;
    	if(psOption){ opt.innerText = fruitList.options[i].innerText;opt.text = fruitList.options[i].text; }
	    else{ opt.label = (fruitList.options[i].text ? fruitList.options[i].text : fruitList.options[i].innerText ); }
	    opt.style.backgroundColor = (psOption ? "" : "#FCEDA6");
	    scrLst.appendChild(opt);
    }	
}

function AddOption( srcListID, psVal, psText, psOption, psSubGrp, psBGYellow)
{
    if(document.getElementById(srcListID) == null ) { return; }
    var opt = ( psOption ? opt=document.createElement('option') : document.createElement('optgroup') );
    opt.value = psVal;
	if(psOption)
	{
        opt.innerText = psText;
        opt.text = psText;
        opt.style.backgroundColor = ( psBGYellow ? "#FCEDA6" : "white" );
	}
	else
	{
        opt.label = psText;
	    opt.style.backgroundColor = (psSubGrp ? "#E0E0E0" : "#FCEDA6");
	    opt.style.fontStyle = "normal";
	}
	document.getElementById(srcListID).appendChild(opt);
}

function CheckToReplace( suprCtrlId, chkVal, srcDivID, ctrlID, numID, valFromID, strStyle, strClass, strWidth, dNum, strChoose, valIndex, valChildID, valAttribute, divErrorId)
{
    var ctrlText = true;
    if( document.getElementById(suprCtrlId).options[document.getElementById(suprCtrlId).selectedIndex].text == chkVal )
        ctrlText = false;
    ReplaceControl( srcDivID, suprCtrlId, ctrlID, numID, valFromID, valIndex, strStyle, ctrlText, strClass, strChoose, strWidth, dNum, valChildID, valAttribute, divErrorId);
    return false;
}

function ReplaceControl( srcDivID, suprCtrlId, ctrlID, numIDs, valFromID, valIndex, strStyle, ctrlText, strClass, strChoose, strWidth, dNum, valChildID, valAttribute, divErrorId)
{
    var tmpDiv = srcDivID.split('|');
    valIndex = ( valIndex ? valIndex : "0");

    var valAttribute1 = valAttribute;

    for( var i = 0; i < tmpDiv.length; i++)
    {
        var strValidate = ( strChoose ? "validate='REQUIRED|SELECT|Oops! you forgot to choose " + (strChoose.split('|'))[i] + "." + ( divErrorId == null ? "" : ( divErrorId.split('|').length > i ? "#" + divErrorId.split('|')[i] : "" ) ) + "'" : "" );
        if( ctrlText )
        {
            var wdTmp = (strWidth.split('|'))[i] ? (((strWidth.split('|'))[i]).split('#'))[1] ? "width:" + (((strWidth.split('|'))[i]).split('#'))[1] : "" : "";
            var vall = ( document.getElementById((valFromID.split('|'))[i]) != null ? (document.getElementById((valFromID.split('|'))[i]).value.split('#'))[parseInt((valIndex.split('|'))[i])] : "" );
            vall = ( vall != null ? vall : "");
            vall = ( vall.indexOf("--") != 0 ? vall : "");
            valAttribute = (dNum != null ? (valAttribute1.split('|')).length > i ? (valAttribute1.split('|'))[i].split('#')[1] : "" : "" );
    	    document.getElementById(tmpDiv[i]).innerHTML = "<input type=\"text\" class=\"" + ((strClass.split('|'))[i] ? ((strClass.split('|'))[i].split('#'))[1] ? ((strClass.split('|'))[i].split('#'))[1] : "" : "") + "\" id=\"" + ((ctrlID.split('|'))[i].split('#'))[1] + "\" value=\""+ vall + "\" " + strValidate + " " + (valAttribute != null? valAttribute : "" ) + " style=\"" + wdTmp + "\" />";
	    }
	    else
	    {
            var wdTmp = (strWidth.split('|'))[i] ? (((strWidth.split('|'))[i]).split('#'))[0] ? "width:" + (((strWidth.split('|'))[i]).split('#'))[0] : "" : "";
	        var vall = (valChildID ? (valChildID.split('|'))[i] : valChildID);
	        valAttribute = (dNum != null ? (valAttribute1.split('|')).length > i ? (valAttribute1.split('|'))[i].split('#')[0] : "" : "" );
	        vall  = ( vall ? "onchange = \"return res_populateFarea(\'" + ((ctrlID.split('|'))[i].split('#'))[0] + "\', \'" + (valChildID.split('|'))[i] + "\', 3, false, true);\"" : "" );
	        document.getElementById(tmpDiv[i]).innerHTML = "<select " + vall + " style=\"width:" + strWidth + "\" id=\"" + ((ctrlID.split('|'))[i].split('#'))[0] + "\" class=\"" + ((strClass.split('|'))[i] ? ((strClass.split('|'))[i].split('#'))[0] ? ((strClass.split('|'))[i].split('#'))[0] : "" : "") + "\" style = \"" + wdTmp + "\" " + strValidate + " " + (valAttribute? (valAttribute.split('#'))[0] : "" ) + "><option style='color:black;background-color:#FCEDA6' value=''>--Select--</option></select>";
	        importXML(((ctrlID.split('|'))[i].split('#'))[0], parseInt((numIDs.split('|'))[i]), dNum, (valFromID.split('|'))[i], (valIndex.split('|'))[i]);
	    }
    }
}

function importXML(strDestID, numID, dNum, valFromID, valIndex, addSelect, valTextOpt)
{
    var xmlDoc;
    prcWt = true;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function () { createTable(strDestID, xmlDoc, valFromID, valIndex, addSelect, valTextOpt); };
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) createTable(strDestID, xmlDoc, valFromID, valIndex, addSelect, valTextOpt); };
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		prcWt = false;
		return;
	}
	dNum = ( dNum ? dNum : 0 );
	var findIn = new Array("../", "../../");
	xmlDoc.load( findIn[dNum] + "getXML.aspx?id=" + DDList[numID]);
}

function createTable(destID, xmlDoc, valFromID, valIndex, addSelect, valTextOpt)
{
	var x = xmlDoc.getElementsByTagName('ITEM');
	if( addSelect ) { AddOption( destID, '', '--Select-', true, false, true); }
	for (var i=0;i<x.length;i++)
	{
		for (var j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			var text = ( x[i].childNodes[j].firstChild != null ? x[i].childNodes[j].firstChild.nodeValue : "");
			var jj = j; var val = "";
			while( jj < x[i].childNodes.length && val == "" )
			{
                jj++;
			    if( x[i].childNodes[jj] == null ){ continue; }
			    if( x[i].childNodes[jj].firstChild == null ){ continue; }
				val = ( x[i].childNodes[jj].firstChild != null ? x[i].childNodes[jj].firstChild.nodeValue : "");
			}
			AddOption( destID, ( val == "" ? ( valTextOpt  ? text : val ) : val ), text, true);
			break;
		}
	}
	if( valFromID != null )
	    SelectDateData(destID, valFromID, valIndex);
	    
	prcWt = false;
}

function AddSelectedData( srcID, destID, limit, valTxt)
{
	if( document.getElementById(destID).options.length == limit ) alert("You can't add more than " + limit + " option(s).");
	else
	{
		var src = document.getElementById(srcID); var added = document.getElementById(destID).options.length;
		for( var i = 0; i < src.length; i++)
		{
			if( src.options[i].selected )
			{
				if( src.options[i].value == "" || src.options[i].value.indexOf("--") >= 0 ) continue;
				if( !IsOptionExists(destID, ( valTxt ? src.options[i].text : src.options[i].value ), valTxt) )
				{
					AddOption( destID, src.options[i].value, src.options[i].text, true);
					added++;
					if( added >= limit ) break;
				}
			}
		}
	}
	return false;
}

function RemoveSelectedData( srcID )
{
	var src = document.getElementById(srcID);
	for( var i = 0; i < src.length; i++)
	{
		if( src.options[i].selected ) { document.getElementById(srcID).removeChild(src.options[i]); i--; }
	}
	return false;
}

function IsOptionExists(srcID, valChk, valTxt)
{
	var src = document.getElementById(srcID); valChk = valChk.toUpperCase();
	for( var  i =0; i < src.length; i++)
	{
		if(valTxt && src.options[i].text.toUpperCase() == valChk ) return true;
		else if( src.options[i].value.toUpperCase() == valChk ) return true;
	}
	return false;
}

function IsValidCtrl(ctrlID)
{
    if(document.getElementById(ctrlID) != null ) return true;
    return false;
}

function OtherTextBox(suprCtrlId, chkVal, srcDiv, ctrlID, val, valAtr, qsWidth)
{
    if( document.getElementById(suprCtrlId).options[document.getElementById(suprCtrlId).selectedIndex].text == chkVal )
    {
        var tmpHTML = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"" + ( qsWidth ? qsWidth : "30%" ) + "\" align=\"right\" valign=\"middle\">&nbsp;</td><td width=\"79%\" align=\"left\" valign=\"middle\" style=\"padding-left:2px\">";
        tmpHTML += "<input id=\"" + ctrlID + "\" maxlength=\"32\" type=\"text\" class=\"textbox_color\" " + (val ? "validate=\"" + ( valAtr ? valAtr + "|" : "REQUIRED|NOTBLANK|" ) + "Oops! You forgot to choose " + val + ".\"" : " " ) + " /> <span class=\"sun\">If Others</span></td></tr></table>";
        document.getElementById(srcDiv).innerHTML = tmpHTML;
    }
    else if(document.getElementById(srcDiv).innerHTML != "&nbsp;") { document.getElementById(srcDiv).innerHTML ="&nbsp;"; }
    return true;
}

function OtherTextBox1(suprCtrlId, chkVal, srcDiv, ctrlID, val, valAtr, qsWidth)
{
       //this.id, 'Others', 'div2', 'txtddlQualificationAwarded', 'Qualification Awarded', null, '80px'
        if( document.getElementById(suprCtrlId).options[document.getElementById(suprCtrlId).selectedIndex].text == chkVal )
        {
       var tmpHTML ='&nbsp;<input id="' + ctrlID + '" maxlength="32" type="text" style="width:150px"  validate="REQUIRED|NOTBLANK|Oops! You forgot to choose" />&nbsp;If Others'
       document.getElementById(srcDiv).innerHTML = tmpHTML;
       }
          
   else if(document.getElementById(srcDiv).innerHTML != "&nbsp;") { document.getElementById(srcDiv).innerHTML ="&nbsp;"; }
    
    return true;
}


function OtherTextBoxEnable(suprCtrlId, chkVal, ctrlID)
{
    if( document.getElementById(suprCtrlId).options[document.getElementById(suprCtrlId).selectedIndex].text == chkVal )
    {
        document.getElementById(ctrlID).removeAttribute("disabled");
        document.getElementById(ctrlID).setAttribute("validate", document.getElementById(ctrlID).getAttribute("validate1"));
    }
    else
    { 
        document.getElementById(ctrlID).setAttribute("disabled", "false");
        document.getElementById(ctrlID).removeAttribute("validate");
    }
    return true;
}
