﻿var ua = navigator.userAgent;
var dom = (document.getElementById) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ie5_5 = ((ua.indexOf("MSIE 5.5")>=0 || ua.indexOf("MSIE 6")>=0) && ua.indexOf("Opera")<0) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var newwindow;
var diffWidth=0; diffHeight=0;

function CreateXmlReq()
{
    var objXmlReq;
	try{
		objXmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			objXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(oc){
			objXmlReq = null;
		}
	}
	if(!objXmlReq && typeof XMLHttpRequest != "undefined"){
		objXmlReq = new XMLHttpRequest();
	}
	return objXmlReq;
}


function getDocObj(elem,parent) {
    var st="";
	if (document.layers) {
			if (parent) {
				st = "document."+parent+".document."+elem;
			} else {
				st = "document."+elem;
			}		            
	} else if (document.all) {
			st = "document.all."+ elem;
	} else if (document.getElementById) {
			st = "document.getElementById('"+elem+"')";
	}
	var obj = eval(st);
	return obj;
}

function sendNewsRequest(data,handle)
{
 var XmlReq = CreateXmlReq();
 var requestUrl = "GetLookBookItems.aspx?rndno=" + Math.random(); 
 if(XmlReq)
 {
  XmlReq.onreadystatechange = function(){ handle(XmlReq); };
  XmlReq.open("POST", requestUrl);
  XmlReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  XmlReq.send(data);
 }
}

function bindDdl(){
    var data = "";
    data += "type=ddlbind"
    sendNewsRequest(data,bindDdlHandle);
}

function bindSampleList(){
    var data = "type=Samplebind"
    var ddl = getDocObj("ddlsamples");
    var dt = ddl.options[ddl.selectedIndex].value;
    data += "&dat=" + dt;
    sendNewsRequest(data,bindSampleListHandle);
}

function bindDdlHandle(XmlReq){
	if(XmlReq.readyState == 4){
	    if(XmlReq.status == 200){
            var objDdl =  getDocObj("ddlsamples");
            var xmlNodes = XmlReq.responseXML.documentElement.getElementsByTagName('samples');	
		    if (xmlNodes.length > 0)
		    {
			    for(var i=0;i<xmlNodes.length;i++)
			    {
				    optn =  new Option(); 
				    optn.text = xmlNodes[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				    optn.value = xmlNodes[i].getElementsByTagName("sample_id")[0].childNodes[0].nodeValue;
				    objDdl.options[objDdl.length] = optn;
				    var sel = xmlNodes[i].getElementsByTagName("selectflag")[0].childNodes[0].nodeValue;
				    if (sel==1){
				        objDdl.selectedIndex = i;
				    }
			    }
			    bindSampleList();
		    }else{
		        //alert("sds");
		    }
		}else{
		    alert(XmlReq.responseText);
		}
	} 
	
}

function bindSampleListHandle(XmlReq){
	if(XmlReq.readyState == 4){
	    if(XmlReq.status == 200){
	    //alert(XmlReq.responseText);
		     var oStr=XmlReq.responseText;
		     var objcon = getDocObj("LookBookContent");
		     objcon.innerHTML = oStr;
		    init();
		    initOther();
		}else{
		    alert(XmlReq.responseText);
		}
	} 
}

function bindApplicationListHandle(XmlReq){
	if(XmlReq.readyState == 4){
	    if(XmlReq.status == 200){
		     var oStr=XmlReq.responseText;
		     var objcon = getDocObj("tdapplication");
		     objcon.innerHTML = oStr;
		     init();
		     initOther();
		}else{
		    alert(XmlReq.responseText);
		}
	} 
}


function ddlApplicationChange(control,sampleid){
     var data = "type=Applicationbind"
	 var Appid = control.options[control.selectedIndex].value;
	 data += "&Application_id=" + Appid;
	 data += "&Sample_id=" + sampleid;
	 sendNewsRequest(data,bindApplicationListHandle);
	 //GetLookBookItems('',Sampleid)
	 //alert(data);
	 //alert(Appid);
}
//function GetLookBookDrop(Comp1) {
//  var XmlReq = CreateXmlReq();
//  var requestUrl="GetLookBookItems.aspx?RndId=" + Math.random();
//  alert("yes");
//  if(XmlReq)
//	{	
//		XmlReq.onreadystatechange = function(){
//			BuildLookBookDrop(XmlReq,Comp1);
//		};		
//		XmlReq.open("POST", requestUrl);
//		XmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//		XmlReq.send("Language=EN&InnoRand=" + Math.random());	
//	}
//}

//function BuildLookBookDrop(XmlReq,Comp1){
//	if(XmlReq.readyState == 4){
//		if(XmlReq.status == 200){			
//			document.getElementById(Comp1).innerHTML = XmlReq.responseText;
//		}else{
//			document.getElementById(Comp1).innerHTML = "<TABLE height='400' width='100%'><TR><TD class='adminblack' align='center' valign='middle'><table><tr><td class='adminblack' align='center'>" + XmlReq.statusText + "...</td></tr></table></TD></TR></TABLE>";
//		}
//	}
//}



function GetLookBookItems(Comp){

	var XmlReq = CreateXmlReq();
    var requestUrl = "GetLookBookItems.aspx?RndId=" + Math.random();    
    if(XmlReq)
	{	
		XmlReq.onreadystatechange = function(){
			BuildLookBook(XmlReq,Comp);
		};		
		XmlReq.open("POST", requestUrl);
		XmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XmlReq.send("Language=EN&InnoRand=" + Math.random());	
	}
}

function BuildLookBook(XmlReq,Comp){
	if(XmlReq.readyState == 4){
		if(XmlReq.status == 200){			
			document.getElementById(Comp).innerHTML = XmlReq.responseText;
		}else{
			document.getElementById(Comp).innerHTML = "<TABLE height='400' width='100%'><TR><TD class='adminblack' align='center' valign='middle'><table><tr><td class='adminblack' align='center'>" + XmlReq.statusText + "...</td></tr></table></TD></TR></TABLE>";
		}
	}
}

function winopen(imgurl,width,height,filename,id,Lang,LookBookId){	
	if(newwindow!=null){
		if(!newwindow.closed){
			newwindow.close();
		}
	}
	//var url = "LookBookImage.htm?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var url = "LookBookInspirational.aspx?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var windowWidth;
    var windowHeight;
	windowHeight = height;
	windowWidth = width;
	var xcen = 100;
	var ycen = 100;
	if (window.screen){
      var avht = screen.availHeight;
      var avwd = screen.availWidth;
      xcen = (avwd - windowWidth) / 2;
      ycen = (avht - windowHeight) / 2;
    }
    var params = "left="+ xcen +",top="+ ycen +",width="+ windowWidth +",height="+ windowHeight +",status=yes,menubar=no,resizable=no,scrollbars=no";
	//newwindow = window.open(url,"Stonemark",params);
	window.location = url;
	return false;
}

function showImage(imgurl,width,height,filename,id,Lang,LookBookId){		
	//var url = "LookBookImage.htm?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	var url = "LookBookInspirational.aspx?url=" + imgurl + "&filename=" + filename + "&id=" + id + "&Lang=" + Lang + "&LookBookId=" + LookBookId;
	//window.location.replace(url);
	window.location = url;
}

function submitfrm(){
	document.forms[0].submit();
}

function QueryString(name){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if(results == null){
		return "";
	}else{
	    return results[1];
	}
}

function GetInnerWidth(){
   var WinInnerWid = 630;  
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerWid = window.innerWidth;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerWid = document.documentElement.clientWidth;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerWid = document.body.clientWidth;    
  }  
  return WinInnerWid;   
}

function GetInnerHeight(){
  var WinInnerHei = 460;  
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerHei = window.innerHeight;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerHei = document.documentElement.clientHeight;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerHei = document.body.clientHeight;    
  }  
  return WinInnerHei;
}

var Width = 0; Height = 0; ran = 0; resizeRate = 6; //Resize rate per minute
var ObjId = "";
function SetWindowDimNCenter(BoundObjId){
    resizeRate = Math.round(60000/resizeRate);    
    ObjId = BoundObjId;
    if(document.getElementById(ObjId)){
        SetDifference();
        SetWidthHeight(BoundObjId);
        windowWidth = Width + diffWidth;
        windowHeight = Height + diffHeight;
        if (window.screen){
            window.moveTo((screen.availWidth - windowWidth) / 2,(screen.availHeight - windowHeight) / 2);
        }
        keepWindowSize();
    }
}
var KeepWindowInt;
function keepWindowSize(){
   if(document.getElementById(ObjId)){
       if(ran!=0){    
        SetWidthHeight(ObjId);
        windowWidth = parseFloat(Width) + parseFloat(diffWidth);
        windowHeight = parseFloat(Height) + parseFloat(diffHeight);
       }
       ran++;
       //document.title = ran + " times runned....";   
       window.resizeTo(windowWidth,windowHeight);
       //window.defaultStatus = "Window is resized with " + windowWidth + "px Width and " + windowHeight + "px Height";
       clearInterval(KeepWindowInt);
       KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
   }else{
       clearInterval(KeepWindowInt);
   }   
}

var checkParentPresentOnce = 50; // milliseconds
function closeMeWhenParentClosed(){
    if(window.opener.closed){
        window.close();
    }
    //initCloseOnBlur();
    //initialized = true;
    setTimeout("closeMeWhenParentClosed();",checkParentPresentOnce);
}

//var initialized = false;
//function initCloseOnBlur(){
//    window.onblur = function(){
//        window.focus();
//    }
//}

function SetWidthHeight(ObjName){
    var Obj = document.getElementById(ObjName);
    if(Obj!=null){
        Width = Obj.offsetWidth;
        Height = Obj.offsetHeight;        
    }else{
        window.defaultStatus = ObjName + " was not found..";
    }
}

function SetDifference(){
    window.resizeTo(500,500);
    diffWidth = 500 - GetInnerWidth();
    diffHeight = 500 - GetInnerHeight();    
}

function closewindow(){
    window.close();
}

onerror = function(err,url,l){
  window.defaultStatus = err + " on line " + l;
  if(KeepWindowInt && ObjId){
    clearInterval(KeepWindowInt);
    KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
  }
  return true;
}


function ShowImage(Url,ImgName,curWidth,curHeight)
{
    try
    {   
        var img=new Image();
        img.src=Url;
        var divLook=document.getElementById('divLookBook');
        var ImgStr="";
        
        var newWidth, newHeight;
        var curWidth, curHeight;
        
        if(curWidth>curHeight)
        {   
	        if (curWidth <= 733)
	        {
		        newWidth= curWidth;
		        newHeight = curHeight;
	        }
	        else
	        {
		        newWidth= 733;
		        newHeight = Math.floor( curHeight * (733 / curWidth));
	        }
	        if(newHeight>550)
	        {
	            newHeight= 550;
		        newWidth = Math.floor( curWidth * (550 / curHeight));
	        }
	    }
	    else
	    {
	        if (curHeight <= 550)
	        {
		        newWidth= curWidth;
		        newHeight = curHeight;
	        }
	        else
	        {
		        newHeight= 550;
		        newWidth = Math.floor( curWidth * (550 / curHeight));
	        }
	        if(newWidth>733)
	        {
	            newWidth= 733;
		        newHeight = Math.floor( curHeight * (733 / curWidth));
	        }
	    }
        
       // alert("cur:" + curWidth + ": " + curHeight + " new :" + newWidth + ": " + newHeight);
//        ImgStr='<table>';
//        ImgStr+='   <tr>';
//        ImgStr+='       <td style="font-weight:bold;" class="defaultfont">';
//        ImgStr+=ImgName;
//        ImgStr+='       </td>';
//        ImgStr+='       <td align="right"><a href="javascript:void(0);" style="text-decoration:underline;color:#797979; font-family:Arial, Helvetica, sans-serif; font-size:12px" onclick="javascript:LookBook.Hide();return false;">Close</a>';
//        ImgStr+='       </td>';
//        ImgStr+='   </tr>';
//        ImgStr+='   <tr>';
//        ImgStr+='       <td colspan="2">';
//        ImgStr+='       <img src="' + Url + '" width="' + newWidth + '" height="' + newHeight + '" id="imgLookBook" alt="" />';
//        ImgStr+='       </td>';
//        ImgStr+='   </tr>';
//        ImgStr+='</table>';
//        divLook.innerHTML=ImgStr;

        var imgLookBook=document.getElementById('imgLookBook');
        imgLookBook.src= Url;
        //alert(Url);
        imgLookBook.style.height=newHeight + "px";
        imgLookBook.style.width=newWidth + "px";
        document.getElementById('ImgName').innerHTML=ImgName;
        
        delete LookBook;
        var LookBook = new Modal("divLookBook",null,"usercontents","#000000","50");
        LookBook.shadowDistance = 4;
        LookBook.shadowopac = 100;
        LookBook.Show();
    }
    catch(err)
    {
        //alert(err);
    }
}


function HideLookBookImage()
{
    LookBook.Hide();
    var imgLookBook=document.getElementById('imgLookBook');
    imgLookBook.src='';
    document.getElementById('ImgName').innerHTML='';
}